package com.dongzili.demo.customer.fulltext.analysis;

import com.alibaba.fastjson.JSONArray;
import com.huaban.analysis.jieba.JiebaSegmenter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @描述： jieba分词工具类
 * @作者： 96511
 * @日期： 2025-01-04 18:01
 */
public class JiebaSupport {

    // 日志打印
    private static final Logger logger = LoggerFactory.getLogger(JiebaSupport.class);

    // 初始化操作
    private static final JiebaSegmenter segment = new JiebaSegmenter();
    public static void main(String[] args) throws Exception {
        String text = "先放后导先厚再薄课标教科书语文五年级下彩色的非洲教学片断与评析";
        String text2 = "先放后导";
        logger.info(JSONArray.toJSONString(participle(text)));

//        List<SegToken> tokens = segment.process(text, JiebaSegmenter.SegMode.INDEX);
//        logger.info(JSONArray.toJSONString(tokens));
    }

    /**
     * 中文分词
     */
    public static List<String> participle(String text){
        String words = removeNonChinese(text);
        List<String> resultList = segment.sentenceProcess(words);
        return resultList.stream().filter(StringUtils::hasText).collect(Collectors.toList());
    }

    /**
     * 保留中文字符和数字
     */
    private static String removeNonChineseAndDigits(String text) {
        // 定义保留中文字符和数字的正则表达式
        String chineseAndDigitRegex = "[^\\p{IsHan}\\p{N}]";
        return text.replaceAll(chineseAndDigitRegex, " ");
    }

    /**
     * 保留中文字符
     */
    private static String removeNonChinese(String text) {
        // 定义保留中文字符和数字的正则表达式
        String chineseAndDigitRegex = "[^\\p{IsHan}]";
        return text.replaceAll(chineseAndDigitRegex, " ");
    }

    private static boolean isPunctuation(String word) {
        // 定义标点符号的正则表达式
        String punctuationRegex = "[\\p{P}\\p{S}]";
        return word.matches(punctuationRegex);
    }


}
